package com.itheima.myscoket.test81;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class MyRunable implements Runnable{
    private Socket socket;
    BufferedOutputStream bos = null;
    public MyRunable(Socket accept) {
        this.socket = accept;
    }

    @Override
    public void run() {
        try {
            //将数据写到本地
           bos = new BufferedOutputStream(new FileOutputStream("day16_网络编程\\Server\\"+ UUID.randomUUID().toString().replace("-","")+".jpg"));
            //得到读取
            InputStream is = socket.getInputStream();
            //将网络中读取的数写出来,写到本地
            BufferedInputStream bis = new BufferedInputStream(is);
            int len;
            while ((len = bis.read()) != -1) {
                bos.write(len);
            }


            //服务器反馈结果
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
